package com.example;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ClientUI implements Message {

    public Client client;
    public String user;

    public ClientUI(){
        client = new Client(this);
        new Thread(client).start();
    }


    public void loginUI(){

        JFrame jf = new JFrame();
        jf.setSize(450,550);
        jf.setTitle("登录界面");
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        FlowLayout flow = new FlowLayout();
        jf.setLayout(flow);


        JLabel user = new JLabel("账号:");
        jf.add(user);


        JTextField jtf = new JTextField();
        Dimension dm = new Dimension(370,30);

        jtf.setPreferredSize(dm);
        jf.add(jtf);
        JLabel pass = new JLabel("密码:");
        jf.add(pass);


        JTextField jtf2 = new JTextField();
        jtf2.setPreferredSize(dm);
        jf.add(jtf2);


        JButton jbu = new JButton("登录");
        jf.add(jbu);
        JButton register = new JButton("注册");
        jf.add(register);


        jf.setVisible(true);


        jbu.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                ClientUI.this.user = jtf.getText();
                String pass = jtf2.getText();

                try {
                    client.os.write(LOGIN);
                    client.sendMsg( ClientUI.this.user +":"+pass);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    }


    public void chatUI(){

        JFrame jf = new JFrame();

        jf.setSize(450,550);
        jf.setTitle(this.user);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setLayout(new FlowLayout());

        JTextArea jta = new JTextArea();
        jta.setPreferredSize(new Dimension(430,300));
        jf.add(jta);

        JTextField jtf = new JTextField();
        jtf.setPreferredSize(new Dimension(430,30));
        jf.add(jtf);

        JButton send = new JButton("发送");
        jf.add(send);

        send.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String sendMsg = jtf.getText();
                try {
                    client.sendMsg(sendMsg);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        jf.setVisible(true);
    }

    public static void main(String[] args){
        ClientUI lo = new ClientUI();
        lo.loginUI();
    }
}
